package com.whfc.emp.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.enums.RecMode;
import com.whfc.common.exception.BizException;
import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.file.properties.FileExpirationRules;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.*;
import com.whfc.emp.constant.RealNamePlatformConstant;
import com.whfc.emp.dao.*;
import com.whfc.emp.dto.*;
import com.whfc.emp.entity.*;
import com.whfc.emp.enums.*;
import com.whfc.emp.manager.CommonEmpConfigManager;
import com.whfc.emp.param.*;
import com.whfc.emp.realNamePlatform.jingZhou.JINGZHOUApi;
import com.whfc.emp.service.AppEmpAttendService;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.service.SysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * @ClasssName AppEmpAttendServiceImpl
 * @Description 人员考勤service
 * @Author hw
 * @Date 2020/12/22 15:22
 * @Version 1.0
 */
@DubboService(interfaceClass = AppEmpAttendService.class, version = "1.0.0", timeout = 60 * 1000)
public class AppEmpAttendServiceImpl implements AppEmpAttendService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService sysDeptService;

    @Autowired
    private AppEmpDayMapper appEmpDayMapper;

    @Autowired
    private AppEmpMapper appEmpMapper;

    @Autowired
    private AppEmpDeptDayMapper appEmpDeptDayMapper;

    @Autowired
    private CommonEmpConfigManager commonEmpConfigManager;

    @Autowired
    private AppEmpAttendRecordMapper appEmpAttendRecordMapper;

    @Autowired
    private AppFaceGateRecordMapper appFaceGateRecordMapper;

    @Autowired
    private FilePathConfig msFilePathConfig;

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private AppSyncRealNameConfigDao appSyncRealNameConfigDao;

    @Autowired
    private AppEmpGroupMapper appEmpGroupMapper;

    @Override
    public PageData<AttendDayEmpDTO> attendDayList(Integer pageNum, Integer pageSize, Date date, Integer deptId, Integer corpId, Integer groupId, String keyword, Integer attendState) {
        logger.info("人员管理-日考勤记录列表,pageNum:{},pageSize:{},date:{},deptId:{},corpId:{},groupId:{},keyword:{},empId:{},attendState:{}", pageNum, pageSize, date, deptId, corpId, groupId, keyword, attendState);
        PageHelper.startPage(pageNum, pageSize);
        List<AttendDayEmpDTO> empList = appEmpDayMapper.selectAttendDayEmp(deptId, date, corpId, groupId, keyword, attendState);
        PageHelper.clearPage();
        String deptName = sysDeptService.getDeptName(deptId);
        for (AttendDayEmpDTO attendDayEmpDTO : empList) {
            attendDayEmpDTO.setDate(date);
            attendDayEmpDTO.setDeptName(deptName);
        }
        return PageUtil.pageData(PageInfo.of(empList));
    }

    @Override
    public AppEmpAttendDayNumDTO attendDayNum(Date date, Integer deptId, Integer corpId, Integer groupId, String keyword, Integer empId) throws BizException {
        logger.info("人员管理-日考勤记录统计,date:{},deptId:{},corpId:{},groupId:{},keyword:{},empId:{}", date, deptId, corpId, groupId, keyword, empId);
        return appEmpDayMapper.selectAttendDayEmpNum(deptId, corpId, groupId, date, keyword, empId);
    }

    @Override
    public PageData<AppEmpAttendMonthDTO> attendMonthList(Integer pageNum, Integer pageSize, Date time, Integer deptId, Integer corpId, Integer groupId, String keyword) {
        logger.info("人员管理-月考勤记录列表,pageNum:{},pageSize:{},time:{},deptId:{},corpId:{},groupId:{},keyword:{}", pageNum, pageSize, time, deptId, corpId, groupId, keyword);
        Date monthBegin = DateUtil.getMonthBegin(time);
        Date monthEnd = DateUtil.getMonthEnd(time);
        PageHelper.startPage(pageNum, pageSize);
        List<AppEmpAttendMonthDTO> list = appEmpDayMapper.selectAttendMonthEmp(deptId, corpId, groupId, monthBegin, monthEnd, keyword);
        PageHelper.clearPage();
        String deptName = sysDeptService.getDeptName(deptId);
        joinAttendMont(monthBegin, monthEnd, list, deptName);
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public OssPathDTO exportMonthReport(EmpMonthReportExportParam request) {
        logger.info("导出人员月考勤记录,request:{}", request);
        // 获取数据
        Integer deptId = request.getDeptId();
        Integer corpId = request.getCorpId();
        Integer groupId = request.getGroupId();
        Date month = request.getMonth();
        String keyword = request.getKeyword();
        Date monthBegin = DateUtil.getMonthBegin(month);
        Date monthEnd = DateUtil.getMonthEnd(month);
        List<AppEmpAttendMonthDTO> list = appEmpDayMapper.selectAttendMonthEmp(deptId, corpId, groupId, monthBegin, monthEnd, keyword);
        String deptName = sysDeptService.getDeptName(deptId);
        joinAttendMont(monthBegin, monthEnd, list, deptName);
        OssPathDTO ossPathDTO = new OssPathDTO();
        try {
            // 获取模板
            ClassPathResource resource = new ClassPathResource("templates/empMonthReportTemplate.xls");
            InputStream templateFileInputStream = resource.getInputStream();
            File tempFile = FileUtil.copyTemplateFile(templateFileInputStream);

            //写数据
            FileInputStream fileInputStream = new FileInputStream(tempFile);
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet sheet = workbook.getSheetAt(0);

            //日期列表
            List<Date> dateList = DateUtil.getDateListBetween(monthBegin, monthEnd);

            // 表格抬头
            HSSFRow row0 = sheet.getRow(0);
            HSSFCell cell0 = row0.createCell(0);
            cell0.setCellValue(DateUtil.formatDate(month, "yyyy年M月人员考勤记录"));
            // 合并单元格
            CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 3 + dateList.size());
            sheet.addMergedRegion(cra);
            CellRangeAddress cra1 = new CellRangeAddress(1, 1, 4, 3 + dateList.size());
            sheet.addMergedRegion(cra1);
            // 居中
            HSSFCellStyle titleStyle = workbook.createCellStyle();
            titleStyle.setAlignment(HorizontalAlignment.CENTER);
            cell0.setCellStyle(titleStyle);

            // 填充表头
            HSSFRow row = sheet.getRow(2);
            for (int i = 0; i < dateList.size(); i++) {
                Date date = dateList.get(i);
                int colIdx = 4 + i;
                HSSFCell cell = row.createCell(colIdx);
                cell.setCellValue(DateUtil.formatDate(date, "d"));
            }

            //填充数据
            int rowIdx = 3;
            for (AppEmpAttendMonthDTO empAttendMonthDTO : list) {
                HSSFRow row1 = sheet.createRow(rowIdx);

                row1.createCell(0).setCellValue(empAttendMonthDTO.getEmpName());
                row1.createCell(1).setCellValue(empAttendMonthDTO.getEmpPhone());
                row1.createCell(2).setCellValue(empAttendMonthDTO.getGroupName());
                row1.createCell(3).setCellValue(empAttendMonthDTO.getAttendDays());
                List<AppEmpDayWorkTimesDTO> dateList1 = empAttendMonthDTO.getDateList();

                for (int i = 0; i < dateList1.size(); i++) {
                    int colIdx = i + 4;
                    //开机时长
                    row1.createCell(colIdx).setCellValue(dateList1.get(i).getWorkTimes());
                }
                rowIdx++;
            }

            //保存excel
            FileOutputStream fos = new FileOutputStream(tempFile);
            workbook.write(fos);
            fos.flush();
            fos.close();

            //上传oss
            String name = msFilePathConfig.getFilePath("emp/tmp", String.format("%s%s人员考勤记录.xls", DateUtil.formatDate(month, "yyyy年MM月"), deptName));
            FileInputStream inputStream = new FileInputStream(tempFile);
            String upload = fileHandler.upload(name, inputStream, FileExpirationRules.oneDay);
            ossPathDTO.setPath(upload);
        } catch (Exception e) {
            logger.error("导出人员月考勤记录失败", e);
        }
        return ossPathDTO;
    }

    @Override
    public OssPathDTO exportDayReport(EmpDayReportExportParam param) throws BizException {
        Integer deptId = param.getDeptId();
        Date date = param.getDate();
        Integer corpId = param.getCorpId();
        Integer groupId = param.getGroupId();
        Integer attendState = param.getAttendState();
        String keyword = param.getKeyword();
        String deptName = sysDeptService.getDeptName(deptId);
        List<AttendDayEmpDTO> empList = appEmpDayMapper.selectAttendDayEmp(deptId, date, corpId, groupId, keyword, attendState);
        OssPathDTO ossPathDTO = new OssPathDTO();
        try {
            String dateStr = DateUtil.formatDate(date, "yyyy年MM月dd日");

            //写数据
            File destFile = File.createTempFile(RandomUtil.getRandomFileName(), ".xls");
            ClassPathResource resource = new ClassPathResource("templates/attendDay.xls");
            ExcelWriter excelWriter = EasyExcel.write(destFile).withTemplate(resource.getInputStream()).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            Map<String, Object> map = new HashMap<>(2);
            map.put("deptName", deptName);
            map.put("date", dateStr);
            excelWriter.fill(map, writeSheet);

            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(empList, fillConfig, writeSheet);
            excelWriter.finish();

            //上传oss
            String filepath = msFilePathConfig.getFilePath(String.format("emp/tmp/%s%s日考勤记录.xls", deptName, dateStr));
            String upload = fileHandler.upload(filepath, new FileInputStream(destFile), FileExpirationRules.DAYS_1);
            ossPathDTO.setPath(upload);
        } catch (Exception e) {
            logger.error("导出日考勤记录失败", e);
        }
        logger.info("导出文件:{}", ossPathDTO.getPath());
        return ossPathDTO;
    }

    @Override
    public PageData<EmpDataDTO> empDayList(Integer deptId, Date startDate, Date endDate, Integer pageNum, Integer pageSize) {
        logger.info("人员日报,pageNum:{},pageSize:{},deptId:{},startDate:{},endDate:{}", pageNum, pageSize, deptId, startDate, endDate);
        PageHelper.startPage(pageNum, pageSize);
        List<EmpDataDTO> list = appEmpDayMapper.selectEmpNumTotalPerDay(deptId, startDate, endDate);
        PageHelper.clearPage();

        //计划出勤人数
        List<EmpAttendPlan> attendPlans = appEmpDeptDayMapper.selectByDeptIdAndTimeRange(deptId, startDate, endDate);
        Map<Date, Integer> attendPlanMap = new HashMap<>(attendPlans.size());
        for (EmpAttendPlan plan : attendPlans) {
            attendPlanMap.put(plan.getDate(), plan.getPlanAttendNum());
        }

        for (EmpDataDTO empData : list) {
            Date date = empData.getDate();
            Integer empTotal = empData.getEmpTotal();
            Integer managerNum = empData.getManagerNum();
            Integer attendNum = empData.getAttendNum();
            Integer planAttendNum = attendPlanMap.containsKey(date) ? attendPlanMap.get(date) : empTotal;
            Double rate = planAttendNum > 0 ? MathUtil.divide(attendNum * 100, planAttendNum, 2) : 0;
            empData.setWorkerNum(empTotal - managerNum);
            empData.setAbsentNum(empTotal - attendNum);
            empData.setPlanAttendNum(planAttendNum);
            empData.setRate(rate);
        }
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public void setDataPlan(EmpAttendPlan plan) throws BizException {
        AppEmpDeptDay record = new AppEmpDeptDay();
        record.setDeptId(plan.getDeptId());
        record.setDate(plan.getDate());
        record.setPlanAttendNum(plan.getPlanAttendNum());
        appEmpDeptDayMapper.insertOrUpdate(record);
    }

    @Override
    public PageData<AppEmpAttendRecordDTO> attendRecord(Integer empId, Date date, Integer pageNum, Integer pageSize, Integer state) {
        logger.info("打卡记录,empId:{},date:{},pageNum:{},pageSize:{},state:{}", empId, date, pageNum, pageSize, state);
//        AttendType attendType = commonEmpConfigManager.getAttendTypeByEmpId(empId);
        PageHelper.startPage(pageNum, pageSize);
        List<AppEmpAttendRecordDTO> list = appEmpAttendRecordMapper.selectByEmpIdAndDate(empId, date, state, null);
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public List<AppEmpAttendRecordDTO> attendRecord(Integer empId, Date date, Integer state) {
        logger.info("打卡记录,empId:{},date:{},state:{}", empId, date, state);
        AttendType attendType = commonEmpConfigManager.getAttendTypeByEmpId(empId);
        return appEmpAttendRecordMapper.selectByEmpIdAndDate(empId, date, state, attendType.getValue());
    }

    @Override
    public List<AppEmpAttendRecordDTO> attendRecord(Integer deptId, Date startTime, Date endTime, AttendType attendType) throws BizException {
        return appEmpAttendRecordMapper.selectAttendRecordByDeptIdAndTime(deptId, startTime, endTime, attendType.getValue());
    }

    @Override
    public PageData<AppFaceGateRecordDTO> attendFaceGateList(Integer pageNum, Integer pageSize, Integer deptId, String keyword, Date startTime, Date endTime) {
        logger.info("人员管理-人脸识别记录(分页),pageSize：{},pageSize：{},deptId：{},keyword：{},startTime：{},endTime：{}", pageSize, pageSize, deptId, keyword, startTime, endTime);
        if (startTime == null) {
            startTime = DateUtil.getDateBegin(new Date());
        }
        if (endTime == null) {
            endTime = new Date();
        }
        //组织机构
        PageHelper.startPage(pageNum, pageSize);
        List<AppFaceGateRecordDTO> list = appFaceGateRecordMapper.selectByDeptIdsAndTime(deptId, startTime, endTime, keyword);
        PageHelper.clearPage();

        return PageUtil.pageData(PageInfo.of(list));
    }


    @Override
    public OssPathDTO faceGateRecordExport(FaceGateRecordExportParam param) {
        logger.info("人员管理-导出识别记录,param：{}", param);
        OssPathDTO ossPathDTO = new OssPathDTO();
        try {
            // 获取模板
            ClassPathResource resource = new ClassPathResource("templates/faceGateRecordTemplate.xls");
            InputStream templateFileInputStream = resource.getInputStream();
            File tempFile = FileUtil.copyTemplateFile(templateFileInputStream);

            // 获取数据
            String keyword = param.getKeyword();
            Date startTime = param.getStartTime();
            Date endTime = param.getEndTime();
            Integer deptId = param.getDeptId();
            if (startTime == null) {
                startTime = DateUtil.getDateBegin(new Date());
            }
            if (endTime == null) {
                endTime = new Date();
            }
            List<AppFaceGateRecordDTO> list = appFaceGateRecordMapper.selectByDeptIdsAndTime(deptId, startTime, endTime, keyword);
            if (CollectionUtils.isEmpty(list)) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "无导出数据");
            }
            String deptName = sysDeptService.getDeptNameById(deptId);
            // 写数据
            FileInputStream fileInputStream = new FileInputStream(tempFile);
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet sheet = workbook.getSheetAt(0);
            HSSFRow row0 = sheet.getRow(0);
            row0.getCell(0).setCellValue(String.format("%s 刷脸记录", deptName));
            // 填充数据
            int rowIdx = 2;
            for (AppFaceGateRecordDTO faceGateRecordDTO : list) {
                HSSFRow row = sheet.createRow(rowIdx);
                Double temperature = faceGateRecordDTO.getTemperature();
                String temperatureStr = "-";
                if (temperature != null) {
                    temperatureStr = String.valueOf(temperature);
                }
                row.createCell(0).setCellValue(faceGateRecordDTO.getEmpName());
                row.createCell(1).setCellValue(StringUtils.isNotEmpty(faceGateRecordDTO.getIdCardNo()) ? faceGateRecordDTO.getIdCardNo() : "-");
                row.createCell(2).setCellValue(StringUtils.isNotEmpty(faceGateRecordDTO.getGroupName()) ? faceGateRecordDTO.getGroupName() : "-");
                row.createCell(3).setCellValue(faceGateRecordDTO.getBirthday() != null ? DateUtil.formatDate(faceGateRecordDTO.getBirthday()) : "-");
                row.createCell(4).setCellValue(faceGateRecordDTO.getGender() != null ? GenderCode.parseByValue(faceGateRecordDTO.getGender()) : "-");
                row.createCell(5).setCellValue(StringUtils.isNotEmpty(faceGateRecordDTO.getPhone()) ? faceGateRecordDTO.getPhone() : "-");
                row.createCell(6).setCellValue(faceGateRecordDTO.getName());
                row.createCell(7).setCellValue(RecMode.parseValue(faceGateRecordDTO.getRecMode()).getDesc());
                row.createCell(8).setCellValue(Direction.parseValue(faceGateRecordDTO.getDirection()).getDesc());
                row.createCell(9).setCellValue(faceGateRecordDTO.getPhotoUrl());
                row.createCell(10).setCellValue(temperatureStr);
                row.createCell(11).setCellValue(DateUtil.formatDate(faceGateRecordDTO.getShowTime(), "yyyy-MM-dd HH:mm:ss"));
                rowIdx++;
            }

            // 保存excel
            FileOutputStream fos = new FileOutputStream(tempFile);
            workbook.write(fos);
            fos.flush();
            fos.close();

            //上传oss
            String name = msFilePathConfig.getFilePath("emp/tmp", String.format("%s识别记录.xls", deptName));
            FileInputStream inputStream = new FileInputStream(tempFile);
            String upload = fileHandler.upload(name, inputStream, FileExpirationRules.oneDay);
            ossPathDTO.setPath(upload);
        } catch (Exception e) {
            logger.error("识别记录导出异常", e);
            e.printStackTrace();
        }
        return ossPathDTO;
    }

    @Override
    public PageData<AppFaceGateRecordDTO> visitorList(Integer pageNum, Integer pageSize, Integer deptId, Date startTime, Date endTime) {
        logger.info("人员管理-访客记录,pageSize：{},pageSize：{},deptId：{},startTime：{},endTime：{}", pageSize, pageSize, deptId, startTime, endTime);
        if (startTime != null) {
            startTime = DateUtil.getDateBegin(startTime);
        }
        if (endTime != null) {
            endTime = DateUtil.getDateEnd(endTime);
        }

        PageHelper.startPage(pageNum, pageSize);
        List<AppFaceGateRecordDTO> list = appFaceGateRecordMapper.selectVisitorList(deptId, startTime, endTime);
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public OssPathDTO visitorListExport(Integer deptId, Date startTime, Date endTime) {
        logger.info("人员管理-智能闸机-导出访客记录,deptId：{},startTime：{},endTime：{}", deptId, startTime, endTime);
        if (startTime != null) {
            startTime = DateUtil.getDateBegin(startTime);
        }
        if (endTime != null) {
            endTime = DateUtil.getDateEnd(endTime);
        }
        List<AppFaceGateRecordDTO> list = appFaceGateRecordMapper.selectVisitorList(deptId, startTime, endTime);
        if (CollectionUtils.isEmpty(list)) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "无导出数据");
        }

        String deptName = sysDeptService.getDeptNameById(deptId);
        OssPathDTO ossPathDTO = new OssPathDTO();
        try {
            // 获取模板
            ClassPathResource resource = new ClassPathResource("templates/faceGateVisitorLog.xls");
            InputStream templateFileInputStream = resource.getInputStream();
            File tempFile = FileUtil.copyTemplateFile(templateFileInputStream);

            //写数据
            FileInputStream fileInputStream = new FileInputStream(tempFile);
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet sheet = workbook.getSheetAt(0);
            HSSFRow row0 = sheet.getRow(0);
            HSSFCell cell0 = row0.getCell(0);
            cell0.setCellValue(String.format("%s 访客进出记录", deptName));
            int rowIdx = 2;
            for (AppFaceGateRecordDTO appFaceGateRecordDTO : list) {
                HSSFRow row1 = sheet.createRow(rowIdx);
                row1.createCell(0).setCellValue(appFaceGateRecordDTO.getEmpName());
                row1.createCell(1).setCellValue(appFaceGateRecordDTO.getPhone());
                row1.createCell(2).setCellValue(appFaceGateRecordDTO.getName());
                row1.createCell(3).setCellValue(RecModeType.parseValue(appFaceGateRecordDTO.getRecMode()).getDesc());
                row1.createCell(4).setCellValue(Direction.parseValue(appFaceGateRecordDTO.getDirection()).getDesc());
                row1.createCell(5).setCellValue(appFaceGateRecordDTO.getTemperature());
                row1.createCell(6).setCellValue(DateUtil.formatDate(appFaceGateRecordDTO.getShowTime(), DateUtil.DATE_TIME_FORMAT));
                rowIdx++;
            }
            FileOutputStream fos = new FileOutputStream(tempFile);
            workbook.write(fos);
            fos.flush();
            fos.close();
            //上传oss

            String name = msFilePathConfig.getFilePath("emp/tmp", String.format("%s访客进出记录导出列表.xls", deptName));
            FileInputStream inputStream = new FileInputStream(tempFile);
            String upload = fileHandler.upload(name, inputStream, FileExpirationRules.oneDay);
            ossPathDTO.setPath(upload);
        } catch (Exception e) {
            logger.error("导出访客进出记录列表失败", e);
        }
        return ossPathDTO;

    }

    @Override
    public List<AppFaceGateRecordDTO> discernList(Integer deptId, Date startTime, Date endTime, Integer type) {
        if (startTime == null) {
            startTime = DateUtil.getDateBegin(new Date());
        }
        if (endTime == null) {
            endTime = new Date();
        }
        //组织机构
        List<AppFaceGateRecordDTO> list = appFaceGateRecordMapper.discernList(deptId, startTime, endTime, type);
        return list;
    }

    @Override
    public List<AppFaceGateRecordDTO> temperatureAbnormalList(Integer deptId, Date startTime, Date endTime) {
        if (startTime == null) {
            startTime = DateUtil.getDateBegin(new Date());
        }
        if (endTime == null) {
            endTime = new Date();
        }
        //组织机构
        List<AppFaceGateRecordDTO> list = appFaceGateRecordMapper.temperatureAbnormalList(deptId, startTime, endTime);
        return list;
    }

    @Override
    public void syncRecord(AppEmpAttendSyncParam request) throws BizException {
        // 查询实名制配置
        AppSyncRealNameConfig appSyncRealNameConfig = appSyncRealNameConfigDao.selectByDeptIdAndPlatform(request.getDeptId(), RealNamePlatformConstant.JING_ZHOU);
        if (appSyncRealNameConfig==null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "未获取到同步配置");
        }

        List<AppFaceGateRecordDTO> list = appFaceGateRecordMapper.selectByDeptIdAndTimeAndEmp(request.getEmpId(), request.getSyncTime(), request.getDeptId());
        if (CollectionUtils.isEmpty(list)) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "无同步数据");
        }

        for (AppFaceGateRecordDTO appFaceGateRecord : list) {
            // 2 插入打卡记录
            AppEmpAttendRecord empAttendRecord = new AppEmpAttendRecord();
            empAttendRecord.setDeptId(request.getEmpId());
            empAttendRecord.setDirection(appFaceGateRecord.getDirection());
            empAttendRecord.setEmpId(appFaceGateRecord.getEmpId());
            empAttendRecord.setTime(appFaceGateRecord.getShowTime());
            empAttendRecord.setPhoto(appFaceGateRecord.getPhotoUrl());
            // 同步考勤记录
            AppEmp appEmp = appEmpMapper.selectByPrimaryKey(request.getEmpId());
            AppEmpGroup appEmpGroup = appEmpGroupMapper.selectByPrimaryKey(appEmp.getGroupId());
            logger.info("考勤同步荆州实名平台：empId={},empName={},groupCode={}", appEmp.getId(), appEmp.getEmpName(), appEmpGroup.getGroupCode());
            JINGZHOUApi.syncAtte(ImageUtil.base64EncodeAndCompress(appFaceGateRecord.getPhotoUrl(), 40), appEmp.getIdCardNo(), appEmpGroup.getGroupCode(), appSyncRealNameConfig, empAttendRecord);
        }

    }

    private void joinAttendMont(Date monthBegin, Date monthEnd, List<AppEmpAttendMonthDTO> list, String deptName) {
        List<Date> dayListOfMonth = DateUtil.getDayListOfMonth(monthBegin);
        for (AppEmpAttendMonthDTO appEmpAttendMonthDTO : list) {
            appEmpAttendMonthDTO.setDeptName(deptName);
            List<AppEmpDayWorkTimesDTO> dateList = new ArrayList<>(dayListOfMonth.size());
            for (Date date : dayListOfMonth) {
                AppEmpDayWorkTimesDTO appEmpDayWorkTimesDTO = new AppEmpDayWorkTimesDTO();
                appEmpDayWorkTimesDTO.setDate(date);
                appEmpDayWorkTimesDTO.setWorkTimes(0.0);
                dateList.add(appEmpDayWorkTimesDTO);
            }
            //出勤天数
            Integer deptId1 = appEmpAttendMonthDTO.getDeptId();
            Integer empId = appEmpAttendMonthDTO.getEmpId();
            Integer attendDays = appEmpDayMapper.selectAttendDaysByEmpId(deptId1, empId, monthBegin, monthEnd);
            appEmpAttendMonthDTO.setAttendDays(attendDays);
            //每日工时
            List<AppEmpDayWorkTimesDTO> dateList2 = appEmpDayMapper.selectWorkTimesByEmpId(empId, monthBegin, monthEnd);
            for (AppEmpDayWorkTimesDTO appEmpDayWorkTimesDTO : dateList) {
                for (AppEmpDayWorkTimesDTO appEmpDayWorkTimesDTO2 : dateList2) {
                    if (appEmpDayWorkTimesDTO.getDate().getTime() == appEmpDayWorkTimesDTO2.getDate().getTime()) {
                        appEmpDayWorkTimesDTO.setWorkTimes(appEmpDayWorkTimesDTO2.getWorkTimes());
                    }
                }
            }
            appEmpAttendMonthDTO.setDateList(dateList);
        }
    }


}
